package newl;

import static java.lang.System.out;
import java.awt.event.KeyEvent;
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.swing.JTextField;

public class QuizTest{
	

	static Connection cn; 
	static Statement stmt; 
	static ResultSet rs; 
	static String db = "partners";
	//static String table = "p_login";
	
	//String rs = null;
	
	public static void main(String[] args) 
	{
		QuizTest base = new QuizTest();
		try 
		{ 
		String userName="root"; 
		String password="1"; 
		String url="jdbc:mysql://localhost:3306/" + db;
		Class.forName ("com.mysql.jdbc.Driver").newInstance (); 
		cn = DriverManager.getConnection(url,userName,password); 
		}
		catch(Exception e){System.out.println(e);} 
		
		
		//base.delete("p_login");
		base.insert("p_login");
		base.select("p_login");
		
		
	}
	
	public void delete(String table)
	{
		try 
		{ 		
		stmt = cn.createStatement(); 
		stmt.executeUpdate("DELETE FROM " + table + " Where 1=1");
		stmt.close();
		} 
		catch(Exception e){System.out.println(e);} 
	}
	
	
	public void insert(String table)
	{
		String value1;
		String value2;
		
		out.println("type values to insert");
		
		try 
		{ 
			stmt = cn.createStatement(); 
			rs = stmt.executeQuery("select * from "+table); 
			BufferedReader reader = new BufferedReader
					( new InputStreamReader(System.in) );
		
		// ввод первого значения в таблицу
		check_exception:
			{
			out.println("first value 1 : ");
		
		
				value1 = reader.readLine();
				while(value1.length()>255)
				{
					out.println("value 1 is too long! try one more time: ");
					value1 = reader.readLine();
				} 
				
				while(rs.next()) 
				{
					if(value1.equals(rs.getString("P_Id")))
					{
						out.println("this E-mail is already here! try another: ");
						break check_exception;
						
					}
					
				}
			}
		
		
		
		
				
		// ввод второго значения в таблицу
		out.println("second value 2 : ");
		value2 = reader.readLine();
		while(value2.length()>255)
		{
			out.println("value 2 is too long! try one more time: ");
			value2 = reader.readLine();
		} 
		
		
		stmt.executeUpdate("INSERT INTO " + "`" + table + "` (`P_Id`, `Pass`) " + "VALUES ('" + value1 + "', '" + value2 +"' )"); 
		stmt.close();		
		} 
		catch(Exception e){System.out.println(e);} 
		
	}

	public void select(String table)
	{
		try 
		{ 			
		stmt = cn.createStatement(); 
		rs = stmt.executeQuery("select * from "+table); 
		while(rs.next()) 
		System.out.println(rs.getString("P_id") + " " + rs.getString("Pass")); 
		stmt.close();
		} 
		catch(Exception e){System.out.println(e);} 
	}
}

